package org.dromara.datacheck.mytest;

import java.util.*;

public class DocumentMatcher {
    public static void main(String[] args) {
        // 聚水潭单据列表
        List<Document> jushuitanDocuments = Arrays.asList(
            new Document("1872668", "HC102100000000-CN"),
            new Document("1913707", "HC100200000000-US"),
            new Document("1913707", "HC601300000000-US"),
            new Document("1913707", "HC105100000000-US"),
            new Document("1913707", "HC603200050000-US"),
            new Document("1913707", "HC122200000000-US"),
            new Document("1913707", "HC100100000000-US"),
            new Document("1913707", "HC506100000000-US"),
            new Document("1913707", "HC520300000000-US"),
            new Document("1913707", "HC520200000000-US"),
            new Document("1913707", "HC507200000000-US"),
            new Document("1913707", "HC122100000000-US")
        );

        // 金蝶单据列表
        List<Document> jindieDocuments = Arrays.asList(
            new Document("1913707-001-QT", "HC603200050000-US"),
            new Document("1913707-007-QT", "HC100100000000-US"),
            new Document("1913707-007-QT", "HC100200000000-US"),
            new Document("1913707-007-QT", "HC105100000000-US"),
            new Document("1913707-007-QT", "HC122100000000-US"),
            new Document("1913707-007-QT", "HC122200000000-US"),
            new Document("1913707-007-QT", "HC507200000000-US"),
            new Document("1913707-007-QT", "HC506100000000-US"),
            new Document("1913707-007-QT", "HC520300000000-US"),
            new Document("1913707-007-QT", "HC520200000000-US"),
            new Document("1913707-007-QT", "HC601300000000-US")
//            new Document("1872668-001-QT", "HC102100000000-CN")
        );

        // 校验两组单据是否一一对应
        Map<String, Document> jushuitanMap = createDocumentMap(jushuitanDocuments);
        Map<String, Document> jindieMap = createDocumentMap(jindieDocuments);

        // 找出不对应的单据
        List<Document> missingInJindie = findMissingDocuments(jushuitanMap, jindieMap);
        List<Document> missingInJushuitan = findMissingDocuments(jindieMap, jushuitanMap);

        if (missingInJindie.isEmpty() && missingInJushuitan.isEmpty()) {
            System.out.println("两组单据一一对应");
        } else {
            System.out.println("两组单据不一一对应");
            if (!missingInJindie.isEmpty()) {
                System.out.println("金蝶缺少的单据：");
                missingInJindie.forEach(doc -> System.out.println(doc));
            }
            if (!missingInJushuitan.isEmpty()) {
                System.out.println("聚水潭缺少的单据：");
                missingInJushuitan.forEach(doc -> System.out.println(doc));
            }
        }
    }

    public static Map<String, Document> createDocumentMap(List<Document> documents) {
        Map<String, Document> documentMap = new HashMap<>();
        for (Document doc : documents) {
            String key = doc.getDocumentNumber().split("-")[0] + "-" + doc.getMaterialCode();
            documentMap.put(key, doc);
        }
        return documentMap;
    }

    public static List<Document> findMissingDocuments(Map<String, Document> sourceMap, Map<String, Document> targetMap) {
        List<Document> missingDocuments = new ArrayList<>();
        for (Map.Entry<String, Document> entry : sourceMap.entrySet()) {
            if (!targetMap.containsKey(entry.getKey())) {
                missingDocuments.add(entry.getValue());
            }
        }
        return missingDocuments;
    }

    static class Document {
        private String documentNumber;
        private String materialCode;

        public Document(String documentNumber, String materialCode) {
            this.documentNumber = documentNumber;
            this.materialCode = materialCode;
        }

        public String getDocumentNumber() {
            return documentNumber;
        }

        public String getMaterialCode() {
            return materialCode;
        }

        @Override
        public String toString() {
            return "单据编号: " + documentNumber + ", 物料编码: " + materialCode;
        }
    }
}
